<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Slam defense</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-qc.pattern-based-caching.html">Pattern based caching</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-qc.cache-candidates.html">Finding cache candidates</a></div>
 <div class="up"><a href="mysqlnd-qc.quickstart.html">Quickstart and Examples</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mysqlnd-qc.slam-defense" class="section">
  <h2 class="title">Slam defense</h2>
  <p class="para">
   A badly designed cache can do more harm than good. In the worst case a cache
   can increase database server load instead of minimizing it. An overload situation
   can occur if a highly shared cache entry expires (cache stampeding).
  </p>
  <p class="para">
   Cache entries are shared and reused to a different degree depending on
   the storage used. The default storage handler stores cache entries in process memory.
   Thus, a cache entry can be reused for the life-span of a process. Other PHP
   processes cannot access it. If Memcache is used, a cache entry can be shared
   among multiple PHP processes and even among multiple machines, depending on the
   set up being used.
  </p>
  <p class="para">
   If a highly shared cache entry stored, for example, in Memcache expires, many
   clients gets a cache miss. Many client requests can no longer be served from
   the cache but try to run the underlying query on the database server. Until
   the cache entry is refreshed, more and more clients contact the database server.
   In the worst case, a total lost of service is the result.
  </p>
  <p class="para">
   The overload can be avoided using a storage handler which limits the reuse of cache
   entries to few clients. Then, at the average, its likely that only a limited number
   of clients will try to refresh a cache entry concurrently.
  </p>
  <p class="para">
   Additionally, the built-in slam defense mechanism can and should be used. If
   slam defense is activated an expired cache entry is given an extended life time.
   The first client getting a cache miss for the expired cache entry tries to
   refresh the cache entry within the extended life time. All other clients requesting
   the cache entry are temporarily served from the cache although the original
   <em>TTL</em> of the cache entry has expired. The other clients will
   not experience a cache miss before the extended life time is over.
  </p>
  <p class="para">
   <div class="example" id="example-1824">
    <p><strong>Example #1 Enabling the slam defense mechanism</strong></p>
    <div class="example-contents">
<div class="inicode"><pre class="inicode">mysqlnd_qc.slam_defense=1
mysqlnd_qc.slam_defense_ttl=1</pre>
</div>
    </div>

   </div>
  </p>
  <p class="para">
   The slam defense mechanism is enabled with the PHP configuration directive
   <a href="mysqlnd-qc.configuration.html#ini.mysqlnd-qc.slam-defense" class="link"><em>mysqlnd_qc.slam_defense</em></a>.
   The extended life time of a cache entry is set with
   <a href="mysqlnd-qc.configuration.html#ini.mysqlnd-qc.slam-defense-ttl" class="link"><em>mysqlnd_qc.slam_defense_ttl</em></a>.
  </p>
  <p class="para">
   The function
   <span class="function"><a href="function.mysqlnd-qc-get-core-stats.html" class="function">mysqlnd_qc_get_core_stats()</a></span> returns an array of
   statistics. The statistics <em>slam_stale_refresh</em> and
   <em>slam_stale_hit</em> are incremented if slam defense takes place.
  </p>
  <p class="para">
   It is not possible to give a one-fits-all recommendation on the slam defense
   configuration. Users are advised to monitor and test their setup and derive
   settings accordingly.
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-qc.pattern-based-caching.html">Pattern based caching</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-qc.cache-candidates.html">Finding cache candidates</a></div>
 <div class="up"><a href="mysqlnd-qc.quickstart.html">Quickstart and Examples</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
